package com.mazaiting.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mazaiting.manager.domain.entity.SysRoleMenu;
import com.mazaiting.manager.mapper.ISysRoleMenuMapper;
import com.mazaiting.manager.service.ISysRoleMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色关联的菜单 服务实现类
 * </p>
 *
 * @author 麻再挺
 * @since 2022-09-06
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysRoleMenuServiceImpl extends ServiceImpl<ISysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    @Override
    public boolean save(Long roleId, Long menuId) {
        return save(new SysRoleMenu(roleId, menuId));
    }

    @Override
    public List<Long> listMenuIdsByRoleId(Long roleId) {
        return baseMapper.listMenuIdsByRoleId(roleId);
    }

    @Override
    public boolean deleteByRoleId(Long roleId) {
        // 如果查询出来的列表数据为空, 则返回成功
        List<Long> idList = lambdaQuery().select(SysRoleMenu::getId).eq(SysRoleMenu::getRoleId, roleId).list().stream().mapToLong(SysRoleMenu::getId).boxed().toList();
        if (CollectionUtil.isEmpty(idList)) return true;
        // 批量删除
        return removeBatchByIds(idList);
    }

    @Override
    public boolean save(Long roleId, List<Long> menuIds) {
        // 如果菜单 ID 为空, 则返回成功
        if (CollectionUtil.isEmpty(menuIds)) return true;
        return saveBatch(menuIds.stream().map(menuId -> new SysRoleMenu(roleId, menuId)).toList());
    }
}
